#include "weapon.h"

weapon::weapon(){
	markedForDeletion = false;
	w_damage=0;
	w_spriteSize=32;
	w_maxSpeedX=4.0;
	w_maxSpeedY=4.0;
	w_Offset.x =0.0;
	w_Offset.y =0.0;
	w_velocity.x =0.0;
	w_velocity.y=0.0;
	w_position.x=0.0;
	w_position.y=0.0;
	w_localPosition.x=0.0;
	w_localPosition.y=0.0;
	w_boundsOffset=0;
	w_boundsSize=0;
	w_boundingBox.top=w_position.y + (float)w_boundsOffset;
	w_boundingBox.left=w_position.x + (float)w_boundsOffset;
	w_boundingBox.bottom=w_boundingBox.top + (float)w_boundsSize;
	w_boundingBox.right=w_boundingBox.left+(float)w_boundsSize;
	playerOwned=false;

	w_sprite = NULL;
	w_spriteAnim = NULL;
}

weapon::weapon(const weapon& W){
	markedForDeletion = W.markedForDeletion;
	w_damage = W.w_damage;
	w_spriteSize = W.w_spriteSize;
	w_maxSpeedX = W.w_maxSpeedX;
	w_position = W.w_position;
	w_localPosition = W.w_localPosition;
	w_Offset = W.w_Offset;
	w_velocity = W.w_velocity;
	w_facingRight = W.w_facingRight;
	w_boundingBox = W.w_boundingBox;
	w_animStart = W.w_animStart;
	w_animEnd = W.w_animEnd;
	playerOwned = W.playerOwned;
	
	w_sprite = W.w_sprite;
	w_spriteAnim = W.w_spriteAnim;
}

weapon::~weapon(){
	lnSpriteDestroy(w_sprite);
}

void weapon::move(int direction){
	if(direction==1){
		//move right
		w_velocity.x += w_maxSpeedX;
		w_facingRight=1;
	}else if(direction==0){
		//move left
		w_velocity.x -= w_maxSpeedX;
		w_facingRight=0;
	}else if(direction == 2){
		//down
		w_facingRight = 3;
		w_velocity.y += w_maxSpeedY;
	}else if(direction == 3){
		//up
		w_facingRight = 2;
		w_velocity.y -= w_maxSpeedY;
	}
}

void weapon::handleAnimation(){
	//if((w_facingRight ==0) || (w_facingRight == 1)){
	//	w_spriteAnim->animStart = w_animStart;
	//	w_spriteAnim->animEnd = w_animEnd;
	//	lnSpriteAnimLoop(w_sprite,ANIMTYPE_LOOP,0,1);
	//}else if((w_facingRight == 2) || (w_facingRight == 3)){
	//	w_spriteAnim->animStart = 39;//w_animStart+1;
	//	w_spriteAnim->animEnd = 39;//w_animEnd+1;
	//	lnSpriteAnimLoop(w_sprite,ANIMTYPE_LOOP,0,1);
	//}
	if(w_facingRight == 1){
		w_spriteAnim->animStart = w_animStart;
		w_spriteAnim->animEnd = w_animEnd;
		lnSpriteAnimLoop(w_sprite,ANIMTYPE_LOOP,0,1);
		w_sprite->hFlip=true;
	}else if(w_facingRight == 0){
		w_spriteAnim->animStart = w_animStart;
		w_spriteAnim->animEnd = w_animEnd;
		lnSpriteAnimLoop(w_sprite,ANIMTYPE_LOOP,0,1);
		w_sprite->hFlip=false;
	}else if(w_facingRight == 2){
		w_spriteAnim->animStart = w_animStart+1;
		w_spriteAnim->animEnd = w_animEnd+1;
		lnSpriteAnimLoop(w_sprite,ANIMTYPE_LOOP,0,1);
		w_sprite->vFlip=false;
	}else if(w_facingRight == 3){
		w_spriteAnim->animStart = w_animStart+1;
		w_spriteAnim->animEnd = w_animEnd+1;
		lnSpriteAnimLoop(w_sprite,ANIMTYPE_LOOP,0,1);
		w_sprite->vFlip=true;
	}
}

void weapon::display(){
	w_localPosition.x = w_position.x - w_Offset.x;
	w_localPosition.y = w_position.y - w_Offset.y;
	w_sprite->isHidden=false;
	/*if(w_localPosition.x < -32 || w_localPosition.y < -32){
		w_sprite->isHidden=true;
	}else if(w_localPosition.x > SCREEN_WIDTH || w_localPosition.y > SCREEN_HEIGHT){
		w_sprite->isHidden=true;
	}else{
		w_sprite->isHidden=false;
	}*/
	w_sprite->posX = (int)w_localPosition.x;
	w_sprite->posY = (int)w_localPosition.y;
	
}

void weapon::createWeaponSprite(const void *sprData, const void *sprPalette,int spriteSize,int start,int end,int boundsOffset,int boundSize){
	w_animStart=start;
	w_animEnd=end;
	if(spriteSize == 32){
		w_sprite = lnSpriteCreate(SCREEN_MAIN, SPRSIZE_32x32, COLMODE_16, sprData, sprPalette);
		w_spriteAnim = lnSpriteAnimBind(w_sprite, sprData, 1, 7);
	}else if(spriteSize == 16){
		w_sprite = lnSpriteCreate(SCREEN_MAIN, SPRSIZE_16x16, COLMODE_16, sprData, sprPalette);
		w_spriteAnim = lnSpriteAnimBind(w_sprite, sprData, 1, 7);
	}else if(spriteSize == 8){
		w_sprite = lnSpriteCreate(SCREEN_MAIN, SPRSIZE_8x8, COLMODE_16, sprData, sprPalette);
		w_spriteAnim = lnSpriteAnimBind(w_sprite, sprData, 1, 7);
	}
}

void weapon::destroyWeaponSprite(){
	lnSpriteDestroy(w_sprite);
}

void weapon::updateWeapon(){
	if(w_velocity.x > w_maxSpeedX){
		w_velocity.x = w_maxSpeedX;
	}else if(w_velocity.x < -w_maxSpeedX){
		w_velocity.x = -w_maxSpeedX;
	}

	w_position.x += w_velocity.x;
	w_position.y += w_velocity.y;

	w_boundingBox.top=w_position.y + (float)w_boundsOffset;
	w_boundingBox.left=w_position.x + (float)w_boundsOffset;
	w_boundingBox.bottom=w_boundingBox.top + (float)w_boundsSize;
	w_boundingBox.right=w_boundingBox.left+(float)w_boundsSize;

	//printf("%f %f\n",(float)w_position.x,(float)w_position.y);
	//w_sprite->isHidden=false;
	handleAnimation();
	display();
}

void weapon::setOffset(tFloat posX,tFloat posY){
	w_Offset.x=posX;
	w_Offset.y=posY;
}

void weapon::setPos(tFloat posX, tFloat posY){
	w_position.x=posX;
	w_position.y=posY;
}

void weapon::setHidden(bool hidden){
	if(hidden){
		w_sprite->isHidden=true;
	}else{
		w_sprite->isHidden=false;
	}
}

int weapon::getDamage(){
	return w_damage;
}
void weapon::setDamage(int damage){
	w_damage=damage;
}

void weapon::setPlayerOwned(bool owned){
	playerOwned = owned;
}

bool weapon::getPlayerOwned(){
	return playerOwned;
}

t2DVector weapon::getOffset(){
	return w_Offset;
}

t2DVector weapon::getPos(){
	return w_position;
}

BoundingBox weapon::getBounds(){
	return w_boundingBox;
}